﻿using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace LunchOMatic
{
    public partial class frmMain : Form
    {
        public List<Image> logos = new List<Image>();

        public frmMain()
        {
            InitializeComponent();
        }

        public void RandomizeList(List<string> arrayList)
        {

            if (arrayList == null) { return; }
            int count = arrayList.Count;
            for (int i = 0; i < count; i++)
            {
                string tmp = arrayList[i];
                arrayList.RemoveAt(i);
                arrayList.Insert(GetRandomNumber(0, count), tmp);
            }
        }

        private int GetRandomNumber(int minValue, int maxValue)
        {
            // Use a 4-byte array to fill it with random bytes and convert it then
            // to an integer value.
            byte[] randomBytes = new byte[4];

            // Generate 4 random bytes.
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(randomBytes);

            // Convert 4 bytes into a 32-bit integer value.
            int seed = (randomBytes[0] & 0x7f) << 24 |
                        randomBytes[1] << 16 |
                        randomBytes[2] << 8 |
                        randomBytes[3];

            // Now, this is real randomization.
            Random random = new Random(seed);

            return random.Next(minValue, maxValue);
        }
        
        private void frmMain_Load(object sender, EventArgs e)
        {
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            path = Path.Combine(path, "LunchLogos");

            string[] files = Directory.GetFiles(path, "*.png");
            List<string> filesList = new List<string>(files);

            foreach(string imagePath in files)
                logos.Add(new Bitmap(imagePath));

            Spin();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }

        private void menuItemSpin_Click(object sender, EventArgs e)
        {
            menuItemSpin.Enabled = false;
            int spinCount = GetRandomNumber(3, 6);

            for (int i = 0; i < spinCount; i++)
            {
                Spin();
                Application.DoEvents();
                Thread.Sleep(333);
                Application.DoEvents();
            }

            menuItemSpin.Enabled = true;
        }

        private void Spin()
        {
            int index1 = GetRandomNumber(0, logos.Count);
            int index2 = (index1 + 1) % logos.Count;
            int index3 = (index2 + 1) % logos.Count;

            pictureBoxTop.Image = logos[index1];
            pictureBox.Image = logos[index2];
            pictureBoxBottom.Image = logos[index3];
        }
    }
}